package com.gestocasa.model;

import java.util.UUID;
import java.util.Vector;

public class Attribute implements Comparable<Attribute> {

	private int id;
	private UUID internalId;
	private String name;
	private String type;
	private String defaultValue;
	private int order;
	
	public static final String TYPE_DATE 	= "DATE";
	public static final String TYPE_NUMBER = "NUMBER";
	public static final String TYPE_STRING = "STRING";
	
	public Attribute() {
		this(null);
	}
	
	public Attribute(Attribute a) {
		if(a == null) {
			id = -1;
			internalId = UUID.randomUUID();
			name = "";
			type = TYPE_STRING;
			defaultValue = null;
			order = 0;	
		}
		else {
			id = a.getId();
			internalId = a.getInternalId();
			name = a.getName();
			type = a.getType();
			defaultValue = a.getDefaultValue();
			order = a.getOrder();
		}
	}
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public UUID getInternalId() {
		return internalId;
	}

	public void setInternalId(String internalId) {
		setInternalId((internalId == null) ? UUID.randomUUID() : UUID.fromString(internalId));
	}
	
	public void setInternalId(UUID internalId) {
		this.internalId = internalId;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getDefaultValue() {
		return defaultValue;
	}

	public void setDefaultValue(String defaultValue) {
		this.defaultValue = defaultValue;
	}

	public int getOrder() {
		return order;
	}

	public void setOrder(int order) {
		this.order = order;
	}
	
	public static Vector<String> getTypes() {
		Vector<String> types = new Vector<String>();
		types.add(TYPE_DATE);
		types.add(TYPE_NUMBER);
		types.add(TYPE_STRING);
		return types;
	}

	public boolean equals(Attribute a) {
		return (a == null) ? false : a.getInternalId().equals(internalId);
	}
	
	public int compareTo(Attribute o) {
		if(order == o.getOrder()) {
			if(name == null || o.getName() == null) {
				return 0;
			}
			else {
				return name.compareTo(o.getName());
			}
		}
		else {
			if(order < o.getOrder()) {
				return -1;
			}
			else if(order > o.getOrder()) {
				return 1;
			}
			else {
				return 0;
			}
		}
	}
}
